package service

import (
	"context"
	"errors"
	__ "hospital-srv/basic/proto"
	"hospital-srv/handler/dao"
)

// 设备绑定
func (s *Serve) EquipmentBinding(_ context.Context, in *__.EquipmentBindingReq) (*__.EquipmentBindingResp, error) {

	err := dao.BindingEquipment(in.UserId, in.EquipmentId)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.EquipmentBindingResp{Id: in.UserId}, nil
}

// 设备添加
func (s *Serve) EquipmentAdd(_ context.Context, in *__.EquipmentAddReq) (*__.EquipmentAddResp, error) {
	if in.BrandId <= 0 || in.EquipmentName == "" || in.EquipmentModel == "" || in.Manufacturer == "" {
		return nil, errors.New("参数错误")
	}
	equipment, err := dao.EquipmentAdd(in)
	if err != nil {
		return nil, errors.New("设备添加失败" + err.Error())
	}
	return &__.EquipmentAddResp{
		Id: int32(equipment.Id),
	}, nil
}

// 设备删除
func (s *Serve) EquipmentDel(_ context.Context, in *__.EquipmentDelReq) (*__.EquipmentDelResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("参数错误")
	}
	_, err := dao.EquipmentDel(in)
	if err != nil {
		return nil, errors.New("设备删除失败" + err.Error())
	}
	return &__.EquipmentDelResp{}, nil
}

// 设备修改
func (s *Serve) EquipmentUpdate(_ context.Context, in *__.EquipmentUpdateReq) (*__.EquipmentUpdateResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("参数错误")
	}

	equipment, err := dao.EquipmentUpdate(in)
	if err != nil {
		return nil, errors.New("设备修改失败" + err.Error())
	}
	return &__.EquipmentUpdateResp{
		Id: int32(equipment.Id),
	}, nil
}

// 设备展示
func (s *Serve) EquipmentFind(_ context.Context, in *__.EquipmentFindReq) (*__.EquipmentFindResp, error) {
	find, err := dao.EquipmentFind(in)
	if err != nil {
		return nil, err
	}
	return &__.EquipmentFindResp{
		List: find,
	}, nil
}

// 设备详情
func (s *Serve) EquipmentDetails(_ context.Context, in *__.EquipmentDetailsReq) (*__.EquipmentDetailsResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("参数错误")
	}
	details, err := dao.EquipmentDetails(in)
	if err != nil {
		return nil, err
	}
	return &__.EquipmentDetailsResp{
		Id:             int32(details.Id),
		BrandId:        details.BrandId,
		EquipmentName:  details.EquipmentName,
		EquipmentModel: details.EquipmentModel,
		Manufacturer:   details.Manufacturer,
	}, nil
}
